package homework5.test;

import java.io.IOException;
import java.net.*;
import java.nio.charset.Charset;

/**
 * @author fengq
 */
public class DatagramSocketTest2 {
    public static void main(String[] args) throws IOException {
        InetAddress ia=null;
        byte[] buf=new byte[1024];

        String mess1="你好，我想咨询一个问题";
        System.out.println("我说："+mess1);
        ia = InetAddress.getByName("localhost");
        DatagramPacket datagramPacket = new DatagramPacket(buf,0,1024,ia,8800);
        datagramPacket.setData(mess1.getBytes());
        DatagramSocket datagramSocket = new DatagramSocket();
        datagramSocket.send(datagramPacket);
        datagramSocket.receive(datagramPacket);
        datagramPacket.getData();
        String mess2=new String(datagramPacket.getData(),0,datagramPacket.getLength(), Charset.forName("UTF-8"));
        System.out.println(datagramPacket.getAddress()+"说："+mess2);
        datagramSocket.close();

    }
}
